package com.zhu.service.edu.ThreadLocalService;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhu.commons.utils.R;
import com.zhu.service.edu.feign.OssFeignService;
import com.zhu.service.edu.pojo.EduTeacher;
import com.zhu.service.edu.service.IEduTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class TeacherThreadLocal {

    @Async("taskExecutor")
    public void deleteAvatarById(BaseMapper<EduTeacher> baseMapper, OssFeignService ossFeignService,
                                 IEduTeacherService eduTeacherService, String id) {
        //根据id查出teacher
        EduTeacher eduTeacher = baseMapper.selectById(id);
        if(eduTeacher==null)
        {
            return;
        }
        String avatar = eduTeacher.getAvatar();
        if(StringUtils.isNotBlank(avatar)){
            //截取图片名称删除
            String url = org.apache.commons.lang3.StringUtils.substringAfterLast(avatar, ".com/");
            log.info("url:{}=============",url);
            if(StringUtils.isNotBlank(url)){
                R r = ossFeignService.deleteFile(url);
                System.out.println(r.getSuccess());
                log.info("删除成功");
                return ;
            }
            return;
        }
        return;

    }
}
